home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Text / print / HPDJ400Src.lha / render.c < prev    next >
C/C++ Source or Header  |  2004-05-28  |  12KB  |  336 lines

  1. /*
  2.  *        Render function
  3.  *
  4.  *        HP_DeskJet_400C driver with CMY colour gfx
  5.  */
  6.  
  7. #include "global.h"
  8.  
  9. #define NUMSTARTCMD     7       /* # of cmd bytes before binary data */
  10. #define NUMENDCMD       0       /* # of cmd bytes after binary data */
  11. #define NUMTOTALCMD (NUMSTARTCMD + NUMENDCMD)   /* total of above */
  12. #define NUMCOLORCMD     7       /* Cmds for each color buffer */
  13. #define MAXCOLORBUFS    3
  14. #define STARTCMDSIZE   53
  15. #define QUALITY        41
  16. #define LENGTH          8
  17. #define PLANES         27
  18. #define WIDTH          33
  19.  
  20. /*
  21.         00-04   \033&l0L        perf skip mode off
  22.         05-11   \033&l000F      set paper length
  23.         12-18   \033*t075R      set raster graphics resolution (dpi) (see Density.c)
  24.         19-23   \033*b0M        add compression (Method 0)
  25.         24-29   \033*r01U       Num of raster planes per row (PLANES)
  26.         30-37   \033*r1440S     set raster gfx width
  27.         38-42   \033*r1Q        graphics quality (draft)
  28.         43-47   \033*p2N        set left-to-right graphics
  29.         48-52   \033*r0A        start raster graphics
  30. */
  31. char StartCmd[STARTCMDSIZE+1] = "\033&l0L\033&l000F\033*t075R\033*b0M\033*r01U\033*r1440S\033*r1Q\033*p2N\033*r0A";
  32. static UWORD RowSize, NumColorBufs;
  33. STATIC UBYTE * gamma_table;
  34.  
  35. extern struct PrinterData *PD;
  36. extern struct PrinterExtendedData *PED;
  37. extern UBYTE GammaTables[15][256];
  38.  
  39. /* Render graphics to printer */
  40.  
  41. int Render(long ct, long x, long y, long status)
  42. {
  43.         static ULONG BufSize;
  44.         static UWORD textlength;
  45.         static UBYTE *colors[MAXCOLORBUFS];
  46.         UBYTE *ptr;
  47.         int i, err, papersize, lpi, len;
  48.         long row;
  49.  
  50.         err=PDERR_NOERR;
  51.         switch(status) {
  52.                 case 0 : /* Master Initialization */
  53.                         /*
  54.                                 ct      - pointer to IODRPReq structure.
  55.                                 x       - width of printed picture in pixels.
  56.                                 y       - height of printed picture in pixels.
  57.                         */
  58.                         RowSize = (x + 7) / 8;
  59.                         if (PD->pd_Preferences.PrintShade == SHADE_COLOR) {
  60.                            StartCmd[PLANES] = '-'; /* 3 planes, CMY */
  61.                            StartCmd[PLANES+1] = '3';
  62.                            NumColorBufs = 3;
  63.                         }
  64.                         else
  65.                         {
  66.                            NumColorBufs = 1;
  67.                         }
  68.                         BufSize = RowSize * 2; /* Double buffered */
  69.  
  70.                         lpi=6;
  71.                         if (PD->pd_Preferences.PrintSpacing == EIGHT_LPI)
  72.                            lpi = 8;
  73.  
  74.                         papersize = PD->pd_Preferences.PaperSize;
  75.                 switch (papersize)
  76.                 {
  77.                 case US_LETTER:
  78.                     textlength = 10 * lpi;
  79.                                 break;
  80.                 case US_LEGAL:
  81.                             textlength = 13 * lpi;
  82.                                 break;
  83.                 case EURO_A4:
  84.                                 textlength = (lpi == 8 ? 85 : 65);
  85.                     break;
  86.                 default:   /* Custom size */
  87.                             textlength = PD->pd_Preferences.PaperLength;
  88.                 }
  89.                         /* Set page length */
  90.                         StartCmd[LENGTH] = textlength / 100 | '0';
  91.                         textlength = textlength % 100;
  92.                         StartCmd[LENGTH+1] = textlength / 10 | '0';
  93.                         StartCmd[LENGTH+2] = textlength % 10 | '0';
  94.  
  95.                         row = x;           /* Set Raster Width */
  96.                         StartCmd[WIDTH] = row / 1000 | '0';
  97.                         row = row % 1000;
  98.                         StartCmd[WIDTH+1] = row / 100 | '0';
  99.                         row = row % 100;
  100.                         StartCmd[WIDTH+2] = row / 10 | '0';
  101.                         StartCmd[WIDTH+3] = row % 10 | '0';
  102.  
  103.                         if (PD->pd_Preferences.PrintQuality == LETTER) {
  104.                             StartCmd[QUALITY] = '2';
  105.                         }
  106.  
  107.                         if (PD->pd_Preferences.PrintThreshold>0)
  108.                            gamma_table = GammaTables[PD->pd_Preferences.PrintThreshold];
  109.                         else
  110.                            gamma_table = NULL;
  111.  
  112.                         for (i=0; i< NumColorBufs; i++) {
  113.                             if (!(colors[i] = AllocMem(BufSize, MEMF_PUBLIC)))
  114.                                err = PDERR_BUFFERMEMORY;
  115.                         }
  116.  
  117.                         if (err != PDERR_BUFFERMEMORY) {
  118.                             /* perf skip mode off, set dpi, start raster gfx */
  119.                             err = (*(PD->pd_PWrite))(StartCmd, STARTCMDSIZE-1);
  120.                         }
  121.                         break;
  122.  
  123.                 case 1 : /* Scale, Dither and Render */
  124.                         /*
  125.                                 ct      - pointer to PrtInfo structure.
  126.                                 x       - 0.
  127.                                 y       - row # (0 to Height - 1).
  128.                         */
  129.                         Transfer((struct PrtInfo *)ct, y, colors, RowSize, NumColorBufs);
  130.                         err = PDERR_NOERR; /* all ok */
  131.                         break;
  132.  
  133.                 case 2 : /* Dump Buffer to Printer */
  134.                         /*
  135.                                 ct      - 0.
  136.                                 x       - 0.
  137.                                 y       - # of rows sent (1 to NumRows).
  138.  
  139.                                 White-space strip.
  140.                         */
  141.  
  142.  
  143.                        if (gamma_table != NULL && NumColorBufs == MAXCOLORBUFS)
  144.                           CorrectColours(gamma_table, colors, BufSize);
  145.  
  146.                        if (NumColorBufs == MAXCOLORBUFS)
  147.                           err = DumpColorPrint(colors, BufSize);
  148.                        else
  149.                           err = DumpBWPrint(colors,BufSize);
  150.                         break;
  151.  
  152.                 case 3 : /* Clear and Init Buffer */
  153.                         /*
  154.                                 ct      - 0.
  155.                                 x       - 0.
  156.                                 y       - 0.
  157.                         */
  158.  
  159.                         for (i=0; i<NumColorBufs; i++) {
  160.                             ptr = colors[i];
  161.                             len = BufSize;
  162.                             do {
  163.                                 *ptr++ = 0;
  164.                             } while (--len);
  165.                         }
  166.                         break;
  167.  
  168.                 case 4 : /* Close Down */
  169.                         /*
  170.                                 ct      - error code.
  171.                                 x       - io_Special flag from IODRPReq struct
  172.                                 y       - 0.
  173.                         */
  174.                         err = PDERR_NOERR; /* assume all ok */
  175.                         /* if user did not cancel the print */
  176.                         if (ct != PDERR_CANCEL) {
  177.                                 /* end raster graphics, perf skip mode on */
  178.                                 if ((err = (*(PD->pd_PWrite))
  179.                                         ("\033*rbC\033&l1L", 10  )) == PDERR_NOERR) {
  180.                                         /* if want to unload paper */
  181.                                         if (!(x & SPECIAL_NOFORMFEED)) {
  182.                                                 /* eject paper */
  183.                                                 err = (*(PD->pd_PWrite))
  184.                                                         ("\014", 1);
  185.                                         }
  186.                                 }
  187.                         }
  188.                         /*
  189.                                 flag that there is no alpha data waiting that
  190.                                 needs a formfeed (since we just did one)
  191.                         */
  192.                         PED->ped_PrintMode = 0;
  193.                          /* wait for both buffers to empty */
  194.                         (*(PD->pd_PBothReady))();
  195.                         for (i=0; i<NumColorBufs; i++) {
  196.                           if (colors[i] != NULL)
  197.                                 FreeMem(colors[i], BufSize);
  198.                         }
  199.                         break;
  200.  
  201.                 case 5 : /* Pre-Master Initialization */
  202.                         /*
  203.                                 ct      - 0 or pointer to IODRPReq structure.
  204.                                 x       - io_Special flag from IODRPReq struct
  205.                                 y       - 0.
  206.                         */
  207.                         /* select density */
  208.                         SetDensity(x & SPECIAL_DENSITYMASK);
  209.                         break;
  210.         }
  211.         return(err);
  212. }
  213.  
  214. /* New dump routines which replaces older CompactBuf routine 8/4/02 */
  215.  
  216. int DumpBWPrint(UBYTE *colors[],ULONG BufSize)
  217. {
  218.     /* Dump B&W graphic data */
  219.  
  220.     ULONG size;
  221.     UWORD i=0;
  222.     UBYTE *ptr;
  223.     char cmd[10] = "\033*b0m000W";
  224.     int err, method, mem;
  225.  
  226.     mem = FALSE;
  227.     /* Remove trailing white space */
  228.     size = StripWhiteSpace(colors[0], BufSize);
  229.     if (size>0) {
  230.         /* Compress data */
  231.         if (ptr = AllocMem(BufSize, MEMF_PUBLIC|MEMF_CLEAR)) {
  232.             mem = TRUE;
  233.             i = CompressMethod2(colors[0], ptr, size);
  234.             if (i<=0) {
  235.                 method = 0;
  236.                 i = size;
  237.             } else
  238.                 method = 2;
  239.         }
  240.         else {
  241.             ptr = colors[0];
  242.             method = 0;
  243.             mem = FALSE;
  244.         }
  245.  
  246.         cmd[3] = method | '0';
  247.         cmd[5] = (i/100) | '0';
  248.         cmd[6] = (i - (i/100)*100) / 10 | '0';
  249.         cmd[7] = i % 10 | '0';
  250.     } else {
  251.         method = 0;
  252.         cmd[3] = method | '0';
  253.         cmd[5] = '0';
  254.         cmd[6] = '0';
  255.         cmd[7] = '0';
  256.         ptr=colors[0];
  257.     }
  258.  
  259.     (*(PD->pd_PWrite))(cmd,9);
  260.     err = (*(PD->pd_PWrite))(ptr, i);
  261.     if (mem) FreeMem(ptr, BufSize);
  262.     return err;
  263. }
  264.  
  265. int DumpColorPrint(UBYTE *colors[],ULONG BufSize)
  266. {
  267.     /* Dump Color graphic data */
  268.  
  269.     ULONG size;
  270.     UWORD i=0;
  271.     UBYTE *ptr;
  272.     char startcmd[6] = "\033*b0W";
  273.     char colorcmd[10] = "\033*b0m000V";
  274.     int err, ct, method, mem;
  275.  
  276.     mem = FALSE;
  277.     err = (*(PD->pd_PWrite))(startcmd,5);
  278.  
  279.     for (ct=0; ct<3 && err == PDERR_NOERR; ct++) {
  280.  
  281.         /* Strip trailing White Space */
  282.         size = StripWhiteSpace(colors[ct], BufSize);
  283.         if (size>0) {
  284.             /* Compress data */
  285.             if (ptr = AllocMem(BufSize, MEMF_PUBLIC|MEMF_CLEAR)) {
  286.                 mem = TRUE;
  287.                  i = CompressMethod2(colors[ct], ptr, size);
  288.                  if (i <= 0) {
  289.                     method = 0;
  290.                     i = size;
  291.                  } else
  292.                     method = 2;
  293.             }
  294.             else {
  295.                  ptr = colors[ct];
  296.                  method = 0;
  297.                  mem = FALSE;
  298.             }
  299.  
  300.             colorcmd[3] = method | '0';
  301.             colorcmd[5] = (i/100) | '0';
  302.             colorcmd[6] = (i - (i/100)*100) / 10 | '0';
  303.             colorcmd[7] = i % 10 | '0';
  304.         } else {
  305.             method = 0;
  306.             colorcmd[3] = method | '0';
  307.             colorcmd[5] = '0';
  308.             colorcmd[6] = '0';
  309.             colorcmd[7] = '0';
  310.             ptr = colors[ct];
  311.         }
  312.         (*(PD->pd_PWrite))(colorcmd,9);
  313.         err = (*(PD->pd_PWrite))(ptr, i);
  314.         if (mem) FreeMem(ptr, BufSize);
  315.     }
  316.    return err;
  317. }
  318.  
  319. /* This routine makes a local copy of the colour data and applies
  320.  * gamma correction to it.
  321.  */
  322. VOID CorrectColours(UBYTE *gamma_table, UBYTE *colors[], ULONG width)
  323. {
  324.     LONG x,c;
  325.         UBYTE *ptrstart;
  326.  
  327.         for (c=0; c<MAXCOLORBUFS; c++) {
  328.            ptrstart = colors[c];
  329.  
  330.        for(x = 0 ; x < width ; x++)
  331.        {
  332.         *(ptrstart++) = gamma_table[*ptrstart];
  333.            }
  334.         }
  335. }
  336.